﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;

public partial class BaseData_ClothesCollocationsDetails : System.Web.UI.Page
{
    private string ClothesCollocationImageAbsolutePath
    {
        get
        {
            string clothesCollocationImageAbsolutePath = "";
            if (ViewState["ClothesCollocationImageAbsolutePath"] != null)
            {
                clothesCollocationImageAbsolutePath = ViewState["ClothesCollocationImageAbsolutePath"].ToString();
            }
            return clothesCollocationImageAbsolutePath;
        }
        set
        {
            ViewState["ClothesCollocationImageAbsolutePath"] = value;
        }
    }
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    private string Code
    {
        get
        {
            return Request.QueryString["code"] != null ? Request.QueryString["code"].ToString() : "";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("200300111");
        if (!IsPostBack)
        {
            BindDropCollocationType();
            Bind();
        }
    }

    private void BindDropCollocationType()
    {
        DataTable dtCollocationType = new BLL.DPCategory().GetAllDpCategory().Tables[0];
        dropCollocationType.DataSource = dtCollocationType;
        dropCollocationType.DataTextField = "DP_NAME";
        dropCollocationType.DataValueField = "DP_ID";
        dropCollocationType.DataBind();
    }

    private void Bind()
    {
        Model.ProductsDP productDp = new BLL.ProductsDP().GetModel(this.Code);
        DataSet dst = new BLL.ProductsDPMX().GetByDpId(this.Code);
        if (productDp == null || dst.Tables[0].Rows.Count == 0)
        {
            Response.Write("<script>alert('系统无法获取到该搭配的信息！');window.location.href='ClothesCollocationsMain.aspx'</script>");
        }

        txtCollocationName.Text = productDp.PDP_Name;
        if (productDp.PDP_ISDEL == 1)
        {
            rdoYes.Checked = true;
            rdoNo.Checked = false;
        }
        else if (productDp.PDP_ISDEL == 0)
        {
            rdoYes.Checked = false;
            rdoNo.Checked = true;
        }
        for (int i = 0; i < dropCollocationType.Items.Count; i++)
        {
            if (dropCollocationType.Items[i].Value == productDp.DP_Type.ToString())
            {
                dropCollocationType.SelectedIndex = i;
                break;
            }
        }
        this.ClothesCollocationImageAbsolutePath = productDp.PDP_Photo;
        imgCollocation.ImageUrl = BLL.Common.GetProductCollocationPicPath() + productDp.PDP_Photo;
        txtCollocationDescription.Text = productDp.PDP_Note;

        ListItem item;
        for (int i = 0; i < dst.Tables[0].Rows.Count; i++)
        {
            item = new ListItem();
            item.Text = new BLL.COMMODITY_AUTO().GetModel(dst.Tables[0].Rows[i]["ProductId"].ToString()).CA_NAME + " " + new BLL.GUIGE1().GetMC(dst.Tables[0].Rows[i]["PMX_GG1"].ToString());
            item.Value = dst.Tables[0].Rows[i]["ProductId"].ToString() + "~" + dst.Tables[0].Rows[i]["PMX_GG1"].ToString();
            Hidden1.Value += dst.Tables[0].Rows[i]["ProductId"].ToString() + "~" + dst.Tables[0].Rows[i]["PMX_GG1"].ToString() + ",";

            Select1.Items.Add(item);
        }
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("200300111", "00002"))
        {
            Share.Alert(Page, "无权操作"); return;
        }
        if (!Check.CheckPermission("200300111", "00006"))
        {
            Share.Alert(Page, "无权操作"); return;
        }
        if (File1.Value.Trim() == "")
        {
            imgCollocation.ImageUrl = "";
            this.ClothesCollocationImageAbsolutePath = "";
            Share.Alert(Page, "请选择要上传的文件！"); return;
        }

        System.Drawing.Image m_Image = System.Drawing.Image.FromStream(File1.PostedFile.InputStream, true);

        string filePath = File1.PostedFile.FileName;
        string type = File1.PostedFile.ContentType;
        string filename = DateTime.Now.Ticks.ToString();
        string filenameExtension = filePath.Substring(filePath.LastIndexOf(".")).ToString().Trim();

        string absolutePath = Server.MapPath("../ClothesCollocationImage/" + filename + filenameExtension);

        FileInfo fi = new FileInfo(absolutePath);

        if (!fi.Directory.Exists)
            fi.Directory.Create();

        File1.PostedFile.SaveAs(absolutePath);

        if (!File.Exists(absolutePath))
        {
            imgCollocation.ImageUrl = "";
            this.ClothesCollocationImageAbsolutePath = "";
            Share.Alert(Page, "上传文件失败，请重试！"); return;
        }

        Model.SYSPARAM param = new Model.SYSPARAM();
        param = new BLL.SYSPARAM().GetModel("001");
        string ftpip = param.SP_PARAM13;
        string ftpuser = param.SP_PARAM14;
        string ftppwd = param.SP_PARAM15;
        try
        {
            bool result = false;
            FtpUpDown fud = new FtpUpDown(ftpip, ftpuser, ftppwd);
            if (!fud.IsExistsDirectory("", "ClothesCollocationImage", out result))
            {
                if (result == false)
                {
                    imgCollocation.ImageUrl = "";
                    this.ClothesCollocationImageAbsolutePath = "";
                    Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                }
                if (!fud.MakeDir("", "ClothesCollocationImage"))
                {
                    imgCollocation.ImageUrl = "";
                    this.ClothesCollocationImageAbsolutePath = "";
                    Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                }
            }
            else
            {
                if (result == false)
                {
                    imgCollocation.ImageUrl = "";
                    this.ClothesCollocationImageAbsolutePath = "";
                    Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                }
            }

            if (!fud.Upload("ClothesCollocationImage/", absolutePath, filename + filenameExtension))
            {
                if (File.Exists(absolutePath))
                {
                    File.Delete(absolutePath);
                }
                imgCollocation.ImageUrl = "";
                this.ClothesCollocationImageAbsolutePath = "";
                Share.Alert(this.Page, "上传失败"); return;
            }

            imgCollocation.ImageUrl = absolutePath;
            this.ClothesCollocationImageAbsolutePath = filename + filenameExtension;
            Share.InsertLog("200300111", "00006");
        }
        catch
        {
            imgCollocation.ImageUrl = "";
            this.ClothesCollocationImageAbsolutePath = "";
            Share.Alert(this.Page, "上传失败");
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("200300111", "00002"))
        {
            Share.Alert(Page, "无权操作"); return;
        }
        if (txtCollocationName.Text == "")
        {
            Share.Alert(this.Page, "请填写搭配名称！"); return;
        }
        if (this.ClothesCollocationImageAbsolutePath == "")
        {
            Share.Alert(this.Page, "请上传搭配图片！"); return;
        }
        if (Hidden1.Value == "")
        {
            Share.Alert(this.Page, "请选择搭配包含的商品！"); return;
        }

        

        StringBuilder sbSql = new StringBuilder();

        sbSql.Append("update ProductsDP set PDP_Name='");
        sbSql.Append(txtCollocationName.Text.Trim());
        sbSql.Append("',PDP_Note='");
        sbSql.Append(txtCollocationDescription.Text);
        sbSql.Append("',PDP_Photo='");
        sbSql.Append(this.ClothesCollocationImageAbsolutePath);
        sbSql.Append("',PDP_ISDEL=");
        sbSql.Append(rdoYes.Checked ? 1 : 0);
        sbSql.Append(",DP_Type=");
        sbSql.Append(dropCollocationType.SelectedValue);
        sbSql.Append(" where PDP_ID='");
        sbSql.Append(this.Code);
        sbSql.Append("'|");

        sbSql.Append("delete from ProductsDPMX where PMX_ID='");
        sbSql.Append(this.Code);
        sbSql.Append("'|");

        char[] charToTrim = new char[] { ',' };
        string[] arrProducts = Hidden1.Value.TrimEnd(charToTrim).Split(charToTrim);
        for (int i = 0; i < arrProducts.Length; i++)
        {
            string[] arrProductProperty = arrProducts[i].Split('~');
            if (arrProductProperty.Length >= 2)
            {
                sbSql.Append("INSERT INTO [ProductsDPMX]([PMX_ID],[PMX_MID],[ProductID],[PMX_GG1])VALUES('");
                sbSql.Append(this.Code);
                sbSql.Append("','");
                sbSql.Append(i + 1);
                sbSql.Append("','");
                sbSql.Append(arrProductProperty[0]);
                sbSql.Append("','");
                sbSql.Append(arrProductProperty[1]);
                sbSql.Append("')|");
            }
            else
            {
                sbSql.Append("INSERT INTO [ProductsDPMX]([PMX_ID],[PMX_MID],[ProductID],[PMX_GG1])VALUES('");
                sbSql.Append(this.Code);
                sbSql.Append("','");
                sbSql.Append(i + 1);
                sbSql.Append("','");
                sbSql.Append(arrProductProperty[0]);
                sbSql.Append("','1')|");
            }
        }

        string stringSql = sbSql.ToString();
        char[] charArray = new char[] { '|' };
        string[] sqlCommandArray = stringSql.TrimEnd(charArray).Split(charArray);
        if (new SQLServerDAL.ORDERDOC().Deliver(sqlCommandArray))
        {
            Share.InsertLog("200300111", "00001");
            Response.Write("<script>alert('修改搭配成功！');window.location.href='ClothesCollocationsDetails.aspx?code=" + this.Code + "'</script>");
        }
        else
        {
            Share.Alert(this.Page, "修改搭配失败！");
        }


    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {

    }
}
